package ua.kharkov.khpi.tarasenko.bach.core.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import ua.kharkov.khpi.tarasenko.bach.core.logging.CustomerSession;
import ua.kharkov.khpi.tarasenko.bach.core.logging.TransTypePair;
import ua.kharkov.khpi.tarasenko.bach.core.logging.TransactionType;

public final class SessionUtils {

	private SessionUtils() {
	}

	public static String transactionToString(CustomerSession session) {
		String result = "";

		for (TransTypePair pair : session.getTransactions()) {
			result = result + pair.getItemId() + ":"
					+ pair.getTransactionType().ordinal() + " ";
		}
		return result;
	}

	public static List<TransTypePair> parseTransactions(String accessSeq) {
		StringTokenizer st = new StringTokenizer(accessSeq, ",. ");
		List<TransTypePair> transactions = new ArrayList<TransTypePair>();

		while (st.hasMoreTokens()) {
			String[] pair = st.nextToken().split(":");
			TransTypePair tp = new TransTypePair(Integer.valueOf(pair[0]),
					TransactionType.getTransType(Integer.valueOf(pair[1])));
			
			transactions.add(tp);
		}

		return transactions;
	}
}
